package com.woniu.zsd.permission.service.impl;

import com.woniu.zsd.permission.entity.RolesResource;
import com.woniu.zsd.permission.mapper.RolesResourceMapper;
import com.woniu.zsd.permission.service.IRolesResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.zsd.permission.utils.RoleResourceCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class RolesResourceServiceImpl extends ServiceImpl<RolesResourceMapper, RolesResource> implements IRolesResourceService {

    @Autowired
    private RoleResourceCacheUtil roleResourceCacheUtil;

    @Override
    public int updateRolesResource(Integer id, Integer rolesId, Integer resourceId) {
        if (id!=null){
            baseMapper.updateRolesResource(id,rolesId,resourceId);
            roleResourceCacheUtil.reloadRoleResourcesCache();
            return 1;
        }
        return 0;
    }

    @Override
    public  List<RolesResource> selRolesResource(Integer rolesId) {
        return baseMapper.selRolesResource(rolesId);
    }

    @Override
    public int deleteRolesResource(Integer id) {
        if (id != null) {
            baseMapper.deleteRolesResource(id);
            roleResourceCacheUtil.reloadRoleResourcesCache();
            return 1;
        }
        return 0;
    }

    @Override
    public int addRolesResource(Integer rolesId, Integer resourceId) {
        if (rolesId != null && resourceId != null) {
            baseMapper.addRolesResource(rolesId, resourceId);
            roleResourceCacheUtil.reloadRoleResourcesCache();
            return 1;
        }
        return 0;
    }

    @Override
    public List<RolesResource> findAll() {
        return baseMapper.selectList(null);
    }
}
